package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.entity.ServiceContract;
import com.eastfair.pay.dto.ServiceContractSaveDTO;
import com.eastfair.pay.dto.ServiceContractUpdateDTO;
import com.eastfair.pay.dto.ServiceContractPageQuery;
import com.eastfair.pay.service.ServiceContractService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.pay.vo.ServiceContractVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author clm
 * @date 2022-07-21
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceContract")
@Api(value = "ServiceContract", tags = "")
@PreAuth(replace = "pay:serviceContract:")
public class ServiceContractController extends SuperController<ServiceContractService, Long, ServiceContract, ServiceContractPageQuery, ServiceContractSaveDTO, ServiceContractUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private ServiceContractService serviceContractService;

    @Override
    public void handlerResult(IPage<ServiceContract> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceContract> serviceContractList = list.stream().map((map) -> {
            ServiceContract serviceContract = ServiceContract.builder().build();
            //TODO 请在这里完成转换
            return serviceContract;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceContractList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceContractPageQuery> params) {
        ServiceContractPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * @Author clm
     * @Description //查询合同
     * @Date 10:57 2022/7/22
     * @Param [params]
     * @return com.eastfair.core.base.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.eastfair.pay.vo.ServiceContractVO>>
     **/
    @PostMapping("/queryPage")
    public R<Page<ServiceContractVO>> queryPage(@RequestBody @Validated PageParams<ServiceContractPageQuery> params){

        try {
            Page<ServiceContractVO> page = serviceContractService.queryPage(params);
            return R.success(page);
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 通过合同id获取详情
     * @param id
     * @return
     */
    @GetMapping("/getContractById/{id}")
    public R<ServiceContractVO> getContractById(@PathVariable("id") Long id){
        try {
            ServiceContractVO serviceContractVO = serviceContractService.getContractById(id);
            return R.success(serviceContractVO);
        }catch (BizException biz){
            return R.fail(biz);
        }

    }

}
